<template>
  <div>
    <div style="margin-bottom: 20px;">
      <el-input placeholder="请输入内容" style="width: 300px;" v-model="search">
      </el-input>
      <el-button type="primary" @click="getSearchList">查询</el-button>
    </div>
    <div style="margin:20px">
      <el-table :data="usersList" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="200">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="100">
        </el-table-column>
        <el-table-column prop="gender" label="性别" width="100">
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="100">
        </el-table-column>
        <el-table-column prop="nickname" label="昵称">
        </el-table-column>
        <el-table-column prop="role" label="角色" >
        </el-table-column>
        <el-table-column prop="flag" label="禁用解禁">
          <template slot-scope="scope">
            <el-button @click="flag(scope.row.id)" v-if="scope.row.flag === true" type="primary">禁用</el-button>
            <el-button @click="flag(scope.row.id)" v-else >解禁</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <el-button @click="reset(scope.row.id)" type="danger" >重置密码</el-button>
            <el-button @click="remove(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>

</template>
<script>
export default {
  name: 'users',
  components: {

  },
  data() {
    return {
      search: '',
      usersList: []
    }
  },
  mounted() {
    this.getusers()
  },
  methods: {
    getusers() {
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      this.$axios.get(`/users/backend/list?id=${userInfo.id}`).then(res => {
        if (res.data.code === 1) {
          this.usersList = res.data.list
        }
      })
    },
    getSearchList() {
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      this.$axios.get(`/users/backend/search?id=${userInfo.id}&searchStr=${this.search}`).then(res => {
        if (res.data.code === 1) {
          this.usersList = res.data.list
        }
      })
    },
    remove(id) {
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      this.$axios.post('/users/backend/del', { id: userInfo.id, uid: id }).then(res=>{
        if(res.data.code===1){
          this.$message(res.data.message)
          this.getusers()
        }else{
          this.$message(res.data.message)
        }
      })
    },
    flag(id){
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      this.$axios.post('/users/backend/flag', { id: userInfo.id, uid: id }).then(res=>{
        if(res.data.code===1){
          this.$message(res.data.message)
          this.getusers()
        }else{
          this.$message(res.data.message)
        }
      })
    },
    reset(id){
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      this.$axios.post('/users/backend/reset', { id: userInfo.id, uid: id }).then(res=>{
        if(res.data.code===1){
          this.$message(res.data.message)
          this.getusers()
        }else{
          this.$message(res.data.message)
        }
      })
    }
  }
};
</script>
<style scoped>

</style>